<?php

use yii\helpers\Html;
use yii\grid\GridView;
use kartik\tree\TreeView;
use yii\helpers\Url;
use kartik\tree\Module;

/* @var $this yii\web\View */
/* @var $searchModel common\models\OrgTreeSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

\backend\assets\DataTableAsset::register($this);

$this->title = Yii::t('app', 'Org Trees');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="org-tree-index">

    <?= TreeView::widget([
        // single query fetch to render the tree
        // use the Product model you have in the previous step
//        'query' => Department::find()->addOrderBy('parent_id, lft'),
        'id' => 'department_tree',
        'query' => \common\models\OrgTree::find()->addOrderBy('root, lft'),
        'headingOptions' => ['label' => Yii::t('app', 'Department')],
//        'nodeView'          => '@backend/views/org-tree/_nodeview',
        'nodeAddlViews' => [
            Module::VIEW_PART_1 => '',
            Module::VIEW_PART_2 => '@backend/views/org-tree/_treePart2',
            Module::VIEW_PART_3 => '',
            Module::VIEW_PART_4 => '',
            Module::VIEW_PART_5 => '@backend/views/org-tree/_treePart5',
        ],
//        'nodeActions' => [
//            Module::NODE_MANAGE => Url::to(['/org-tree/my-manage']),
//        ],
        'fontAwesome' => false,     // optional
        'isAdmin' => false,         // optional (toggle to enable admin mode)
        'displayValue' => 1,        // initial display value
        'softDelete' => false,       // defaults to true
        'showCheckbox' => true,
        'cacheSettings' => [
            'enableCache' => true   // defaults to true
        ]
    ]); ?>

    <div class="box">
        <div class="box-header">
            <h3 class="box-title"><?= Yii::t('app', 'Employees')?></h3>
            <div class="box-tools">
                <ul class="pagination pagination-sm no-margin pull-right">
                    <li><a id="dt_refresh_a" href="#" onclick="dt_refresh_btn_on_click();"><i class="fa fa-refresh"></i></a></li>
                </ul>
            </div>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
            <table id="employee_table" class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Number</th>
                    <th>Gender</th>
                    <th>Birthday</th>
<!--                    <th>Email</th>-->
                    <th>Mobile</th>
                    <th>id_card_num</th>
                    <th>native_place</th>
                    <th>blood_type</th>
                    <th>wed_status</th>
                    <th>nationality</th>
                    <th>folk</th>
<!--                    <th>hklx</th>-->
                    <th>weixin</th>
                    <th>position_name</th>
                    <th>position_number</th>
                    <th>labor_relation_state</th>
                    <th>department_name</th>
                    <th>department_number</th>
<!--                    <th>department_display_name</th>-->
                </tr>
                </thead>
                <tbody>
                </tbody>
                <tfoot>
                <tr>
                </tr>
                </tfoot>
            </table>
        </div>
        <!-- /.box-body -->
    </div>
</div>

<script>
    <?php $this->beginBlock('js_running')?>

    var department_id;
    var dt;

    function loadDataTable(department_id) {
        dt = $('#employee_table').DataTable({
            "paging":   true,
            'aLengthMenu': [[10, 20, 50], ['10', '20', '50']],  //设置每页显示记录的下拉菜单
            "ordering": true,
            "info":     true,
            "scrollX": true,
            "processing": true,
            "serverSide": true,
            'stateSave' : true,
            // "searching": false,
            "ajax": {
                "url" : "/kd/ajax-get-list-by-org?department_id=" + department_id,
                'type': 'get'
            },
            'language': {
                "processing": "正在处理中。。。",
                "info": "正在显示第 _START_ 条至第 _END_ 条数据， 总共 _TOTAL_ 条数据",
                // "infoEmpty": "正在显示第 0 条至第 0 条数据， 总共 0 条数据",
                "paginate": {
                    "first": "第一页",
                    "last": "最后一页",
                    "next": "下一页",
                    "previous": "上一页",
                    "search": "过滤记录:"
                },
                'lengthMenu' : '显示 _MENU_ 项结果'
            },
            "order": [[1, 'asc']],
            "columnDefs": [
                { "visible": false,  "targets": [ 0 ] },
                {
                    // 第1列为name
                    "render": function ( data, type, row ) {
                        // return data +' ('+ row[3]+')';
                        return "<a href=\"/kingdee/person/view?id="+encodeURIComponent(row['fid'])+"\" target=\"_blank\">"+data+"</a>";
                    },
                    "targets": 1
                }
            ],
            "columns": [
                { "data": "fid", 'title' : 'id号' },
                { "data": "name", 'title' : '姓名', 'width' : '30px' },
                { "data": "number", 'title' : '员工号' },
                {
                    "data": function ( row, type, val, meta ) {
                        return row.gender == 1 ? '男' : '女';
                    },
                    'title' : '性别' },
                { "data": "birthday", 'title' : '生日', 'width' : '50px' },
                // { "data": "email", 'title' : '邮箱' },
                { "data": "mobile", 'title' : '手机' },
                { "data": "id_card_num", 'title' : '身份证号' },
                { "data": "native_place", 'title' : '家乡' },
                { "data": function ( row, type, val, meta ) {
                        switch (row.blood_type) {
                            case '0':
                                return '未知';
                            case '10':
                                return 'A';
                            case '20':
                                return 'B';
                            case '30':
                                return 'AB';
                            case '40':
                                return 'O';
                            case '90':
                                return '其他';
                            default:
                                return '其他';
                        }
                    },
                    'title' : '血型' },
                { "data": "wed_status", 'title' : '婚姻状况' },
                { "data": "nationality", 'title' : '国籍' },
                { "data": "folk", 'title' : '民族' },
                // { "data": "hklx", 'title' : '户口类型' },
                { "data": "weixin", 'title' : '微信' },
                { "data": "position_name", 'title' : '职位名称' },
                { "data": "position_number", 'title' : '职位编号' },
                { "data": "labor_relation_state", 'title' : '用工关系状态' },
                { "data": "department_name", 'title' : '部门名称' },
                { "data": "department_number", 'title' : '部门编号' }
                // ,{ "data": "department_display_name", 'title' : '部门全称', 'width' : '100px' }
            ]
        });
        return dt;
    }

    $(document).ready(function() {

        dt = loadDataTable('');

        $("#department_tree").on('treeview.selected', function(event, key, data, textStatus, jqXHR) {
            department_id = key;
            dt.ajax.url("/kd/ajax-get-list-by-org?department_id=" + department_id).load();//重新设置url
        });
    });

    function dt_refresh_btn_on_click() {
        dt.ajax.reload();
    }
    <?php $this->endBlock()?>
</script>
<?php
$this->registerJs($this->blocks['js_running'], \yii\web\View::POS_END)
?>
